<template>
  <IBizPanelCtrlPos :modelData="modelData" :controller="controller">
    <template v-if="!!control">
      <IBizControlShell
        :context="controller.context"
        :params="controller.params"
        :modelData="control.modelData"
        v-bind="control.slotProps"
      />
    </template>
  </IBizPanelCtrlPos>
</template>
<script setup lang="ts">
import { IPanelCtrlPos, IControl } from '@ibiz/model-core'
import { IPanelItemController, isSimpleDataContainer } from '@ibiz-template/runtime'

const props = defineProps({
  modelData: {
    type: Object as PropType<IPanelCtrlPos>,
    required: true,
  },
  controller: {
    type: Object as PropType<IPanelItemController>,
    required: true,
  },
  controls: {
    type: Array as PropType<
      {
        modelData: IControl
        slotProps: IData
      }[]
    >,
    default: () => [],
  },
})

const control = computed(() => {
  const { modelData, slotProps } = props.controls.find(
    (ctrl) =>
      ctrl.modelData.name === props.modelData.id || ctrl.modelData.id === props.modelData.id,
  )
  const _props = isSimpleDataContainer(props.controller.parent.model)
    ? { isSimple: true, data: props.controller.data }
    : {}
  return {
    modelData,
    slotProps: { ...slotProps, ..._props },
  }
})
</script>
